﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Net;

namespace MDCAdminTool.Commands
{
    public class RemoveCacheHost : AsyncCommandBindingBase
    {
        public RemoveCacheHost()
            : base()
        {
            Command = Application.Current.Resources["RemoveCacheHost"] as ICommand;
        }

        protected override void OnExecuted( object sender , ExecutedRoutedEventArgs e )
        {
            var host = e.Parameter as CacheHost;
            
			if (ShowConfirmationBox("Do you really want to remove this host?") == MessageBoxResult.Yes &&  host != null )
            {
                host.Stop();
                try
                {
                    IPHostEntry hostInfo = Dns.GetHostEntry(host.Name);
                    host.Cluster.RemoveHost(hostInfo.HostName, host.CachePort);
                }
                catch (Exception ex)
                {
                    host.Output.Add(ex);
                }
            }
        }
        protected override void OnCanExecute( object sender , CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = true;
        }
    }
}